<!--
 * @Description: 
 * @Author: charles
 * @Date: 2020-11-03 12:36:26
 * @LastEditors: YuanYu
 * @LastEditTime: 2021-10-27 14:43:16
-->
<template>
  <div class="home">
    <!-- 头部 -->
    <Header></Header>
    <!-- 体部 -->
    <div class="content">
      <div class="wrapper">
        <div class="carousel">
          <!-- {{carousel}} -->
          <el-carousel :interval="4000" type="card" height="200px">
            <el-carousel-item v-for="cas in carousel" :key="cas.id">
              <img :src="cas.url" alt="" />
            </el-carousel-item>
          </el-carousel>
        </div>
        <div class="plates">
          <div class="plate" v-for="cate in plateData" :key="cate.id">
            <div class="header">
              <div class="title">{{ cate.category.name }}</div>
              <div class="more" @click="toList(cate.category)">
                更多&gt;&gt;
              </div>
            </div>
            <ul class="list" v-if="cate.aritcle.length > 0">
              <li
                class="listTitle"
                @click="toArticle(listItem)"
                v-for="listItem in cate.aritcle"
                :key="listItem.id"
              >
                {{ listItem.title }}
              </li>
            </ul>
            <ul class="noData" v-else>
              暂无数据
            </ul>
          </div>
        </div>
        <div class="topic">
          <div class="topicTop">热门话题&gt;&gt;</div>
          <div class="topicBottom" v-if="topicData.total > 0">
            <div
              class="topicArea"
              v-for="(item, index) in topicData.list"
              :key="index"
            >
              <div class="title">{{ item.name }}</div>
              <div class="name">作者：{{ item.teacher.realname }}</div>
              <div class="time">
                发布时间：{{ item.teacher.registerTime | fmtDate }}
              </div>
              <div class="details" v-html="item.details">
                {{ item.details }}
              </div>
            </div>
            <div class="page">
              <el-pagination
                background
                @current-change="handleCurrentChange"
                :current-page.sync="params.page"
                :page-size="params.pageSize"
                layout="total, prev, pager, next"
                :total="topicData.total"
              >
              </el-pagination>
            </div>
          </div>
          <div class="noData" v-else>暂无数据</div>
        </div>
      </div>
    </div>
    <!-- 底部 -->
    <Footer></Footer>
  </div>
</template>

<script>
// 引入get请求
import { get } from "../utils/request";
import Header from "./components/Header.vue";
import Footer from "./components/Footer";
// 数据逻辑的交互处理
export default {
  components: {
    // 1.引入所需的组件
    // 2.注册
    // Header:Header,
    // 在ES6中 如果key value同名 直接写一个属性就可以了
    Header,
    Footer,
  },
  data() {
    return {
      carousel: [],
      plateData: [],
      params: {
        page: 1,
        pageSize: 1,
      },
      topicData: [],
      // categoryData:[],
    };
  },
  // 方法
  methods: {
    // 发送网络请求 获取数据
    // 引入封装好的工具类 网络请求工具
    // 获取轮播图数据
    // ES6函数的简写方式
    getCarouselData() {
      get("/index/carousel/findAll").then((res) => {
        // console.log(res);
        this.carousel = res.data;
      });
    },
    // getCategroyData() {
    //   get("/index/category/findAll").then(res => {
    //     this.categoryData = res.data;
    //     console.log(res.data);
    //   })
    // },
    getPlateData() {
      get("/index/article/findCategoryArticles").then((res) => {
        // console.log(res.data);
        this.plateData = res.data;
        // this.categoryData = this.plateData.category;
      });
    },
    getTopicData() {
      get("/index/project/pageQuery", this.params).then((res) => {
        // console.log(res.data);
        this.topicData = res.data;
      });
    },
    handleCurrentChange(val) {
      // 使用当前页数与我们分页控制的参数进行赋值
      this.params.page = val;
      this.getTopicData();
    },
    toArticle(listItem) {
      this.$router.push({
        path: "/article",
        query: listItem,
      });
    },
    toList(category) {
      this.$router.push({
        path: "/list",
        query: category,
      });
    },
  },
  created() {
    // alert(111)
    // 在生命周期中调用methods中的方法
    this.getCarouselData();
    this.getPlateData();
    this.getTopicData();
    // this.getCategroyData();
  },
};
// 数据交互逻辑
// 1.在data中定义一个接收数据的容器
// 2.发送网络请求
// 3.
</script>

<style lang="scss">
.content {
  // height: 400px;
  // background-image: url('../../public/img_5.png');
  background-color: rgb(137, 196, 227);
  .wrapper {
    .carousel {
      // margin-top: 10px;
      padding-top: 30px;
      img {
        width: 100%;
      }
    }
    .plates {
      background-color: white;
      display: flex;
      flex-wrap: wrap;
      padding: 10px;
      // margin: 10px;
      .plate {
        margin: 20px;
        height: 250px;
        width: 30%;
        background-color: rgb(228, 224, 224);
        padding: 20px;
        box-sizing: border-box;
        .header {
          display: flex;
          justify-content: space-between;
          .title {
            font-size: 20px;
            color: green;
          }
          .more {
            font-size: 17px;
            color: #999;
            cursor: pointer;
          }
          .more:hover {
            color: green;
          }
        }
        .list {
          li {
            line-height: 2em;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            cursor: pointer;
            box-sizing: border-box;
          }
          .listTitle:hover {
            color: red;
            text-decoration: underline;
          }
        }

        .noData {
          font-size: 25px;
          text-align: center;
          line-height: 150px;
        }
      }
    }
    .topic {
      height: 500px;
      background-color: white;
      .topicTop {
        height: 50px;
        line-height: 50px;
        font-size: 18px;
        color: white;
        background-color: seagreen;
        padding-left: 30px;
      }
      .topicBottom {
        .topicArea {
          height: 420px;
          overflow: scroll;
          .title,
          .name,
          .time {
            text-align: center;
            line-height: 2em;
            font-size: 16px;
          }
        }
        .page {
          height: 30px;
        }
      }
    }
  }
}
</style>